package com.jtpay.payresulthelper.db;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import java.util.List;

import static com.jtpay.payresulthelper.constant.HooKConstant.*;

public class AliPayContentProvider extends ContentProvider {
    private static final UriMatcher sUriMatcher;

    static {
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(_AUTHORITY, AliPayColumn._TABLE_NAME, TYPE_ORDER);
        sUriMatcher.addURI(_AUTHORITY, AliPayColumn._TABLE_NAME + "/#", TYPE_ORDER_ITEM);

        sUriMatcher.addURI(_AUTHORITY, AliPayColumn._TABLE_TEMP_NAME, TYPE_ORDER);
        sUriMatcher.addURI(_AUTHORITY, AliPayColumn._TABLE_TEMP_NAME + "/#", TYPE_ORDER_ITEM);

        sUriMatcher.addURI(_AUTHORITY, AliQueryOrderColumn._TABLE_NAME, TYPE_ORDER);
        sUriMatcher.addURI(_AUTHORITY, AliQueryOrderColumn._TABLE_NAME + "/#", TYPE_ORDER_ITEM);
    }

    private Context mContext;
    private SQLiteDatabase mDatabase;

    @Override
    public boolean onCreate() {
        mContext = getContext();
        AliPayDataHelper aliPayDataHelper = new AliPayDataHelper(mContext);
        mDatabase = aliPayDataHelper.getWritableDatabase();
        return true;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        List<String> path = uri.getPathSegments();
        String table = path.get(0);
        int type = sUriMatcher.match(uri);
        switch (type) {
            case TYPE_ORDER_ITEM:
                String id = path.get(1);
                selection = AliPayColumn._ID + "=" + id
                        + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
                break;
            case TYPE_ORDER:
                break;
            default:
                throw new IllegalArgumentException(uri.toString());
        }
        return mDatabase.query(table, projection, selection, selectionArgs, null, null, sortOrder, null);

    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        int type = sUriMatcher.match(uri);
        switch (type) {
            case TYPE_ORDER:
                return CONTENT_TYPE;
            case TYPE_ORDER_ITEM:
                return CONTENT_TYPE_ITEM;
            default:
                throw new IllegalArgumentException(uri.toString());
        }
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        String table = uri.getPathSegments().get(0);
        long id = mDatabase.insert(table, null, values);
        mDatabase.insertWithOnConflict(table, null, values, SQLiteDatabase.CONFLICT_IGNORE);
        if (id > 0) {
            mContext.getContentResolver().notifyChange(uri, null);
            return uri;
        }
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String where, @Nullable String[] whereArgs) {
        List<String> path = uri.getPathSegments();
        String table = path.get(0);
        int count;
        int type = sUriMatcher.match(uri);
        switch (type) {
            case TYPE_ORDER_ITEM:
                String id = path.get(1);
                count = mDatabase.delete(table, AliPayColumn._ID + "=" + id
                        + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
                break;
            case TYPE_ORDER:
                count = mDatabase.delete(table, where, whereArgs);
                break;
            default:
                throw new IllegalArgumentException(uri.toString());
        }
        if (count > 0) {
            mContext.getContentResolver().notifyChange(uri, null);
        }
        return count;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String
            where, @Nullable String[] whereArgs) {
        List<String> path = uri.getPathSegments();
        String table = path.get(0);
        int count;
        int type = sUriMatcher.match(uri);
        switch (type) {
            case TYPE_ORDER_ITEM:
                String id = path.get(1);
                count = mDatabase.update(table, values, AliPayColumn._ID + "=" + id
                        + (!TextUtils.isEmpty(where) ? " AND (" + where + ')' : ""), whereArgs);
                break;
            case TYPE_ORDER:
                count = mDatabase.update(table, values, where, whereArgs);
                break;
            default:
                throw new IllegalArgumentException(uri.toString());
        }
        if (count > 0) {
            mContext.getContentResolver().notifyChange(uri, null);
        }
        return count;
    }
}
